package com.example.springboottools.temp;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

public class CodeGenerator {

        public static void main(String[] args) {
//            char[] code = "1000".toCharArray();
//            while (!isMaxCode(code)) {
//                System.out.println(new String(code));
//                code = createNextCode(code);
//            }
//            System.out.println(new String(code)); // 打印最后一个编码 ZZZZ
            char[] code = "A998".toCharArray();
            System.out.println(createNextCode(code));
        }

        public static char[] createNextCode(char[] code) {
            createCodeNew(code.length - 1, code);
            if (isAllNinesExceptFirstLetter(code)) {
                int firstNonLetterIndex = findFirstNonLetterIndex(code);
                if (firstNonLetterIndex >= 0) {
                    if (code[firstNonLetterIndex] == '9') {
                        code[firstNonLetterIndex] = 'A';
                        for (int j = firstNonLetterIndex + 1; j < code.length; j++) {
                            code[j] = '0';
                        }
                    } else {
                        code[firstNonLetterIndex] = (char) ((int) code[firstNonLetterIndex] + 1);
                        for (int j = firstNonLetterIndex + 1; j < code.length; j++) {
                            code[j] = '0';
                        }
                    }
                }
            }
            return code;
        }

        public static char[] createCodeNew(int i, char[] chars) {
            if (48 <= (int) chars[i] && (int) chars[i] <= 57) {  // 数字处理
                if ((int) chars[i] == 57) {  // 判断当前字符是否是 9
                    if (i > 0) {
                        chars[i] = '0';
                        createCodeNew(i - 1, chars); // 递归调用
                    } else {
                        chars[i] = 'A';
                        for (int num = 1; num < chars.length; num++) {
                            chars[num] = '0';
                        }
                        return chars;
                    }
                } else {
                    chars[i] = (char) ((int) chars[i] + 1);
                    return chars;
                }
            } else if (65 <= (int) chars[i] && (int) chars[i] <= 90) {  // 字母处理
                if ((int) chars[i] == 90) {  // 判断当前字符是否是 Z
                    if (i > 0) {
                        chars[i] = 'A'; // 重置当前编码为 A
                        createCodeNew(i - 1, chars); // 递归调用
                    } else {
                        chars[i] = 'Z';
                        for (int num = 1; num < chars.length; num++) {
                            chars[num] = 'A';
                        }
                        return chars;
                    }
                } else {
                    chars[i] = (char) ((int) chars[i] + 1);
                    return chars;
                }
            }
            return chars;
        }

        private static boolean isAllNinesExceptFirstLetter(char[] code) {
            if (65 <= (int) code[0] && (int) code[0] <= 90) {  // 第一个字符是字母
                for (int i = 1; i < code.length; i++) {
                    if (code[i] != '9') {
                        return false;
                    }
                }
                return true;
            }
            return false;
        }

        private static int findFirstNonLetterIndex(char[] code) {
            for (int i = 0; i < code.length; i++) {
                if (48 <= (int) code[i] && (int) code[i] <= 57) {  // 数字
                    return i;
                }
            }
            return -1;  // 没有找到非字母字符
        }

        private static boolean isMaxCode(char[] code) {
            for (char c : code) {
                if (c != 'Z') {
                    return false;
                }
            }
            return true;
        }


}
